package www.openteam.com.tool;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;

import java.util.ArrayList;
import java.util.List;

import www.openteam.com.bean.EvalutateAddOrderBean;

public class AdpUtil {


    public static void removeTextWacth(List<EditText> editTextList) {
        int size = editTextList.size();
        for (int i = 0; i < size; i++) {
            if (editTextList.get(i).getTag() instanceof TextWatcher) {
                editTextList.get(i).removeTextChangedListener((TextWatcher) editTextList.get(i).getTag());
            }
        }
    }

    public static void setEditTextWatcher(EditText editText, final IBeanSetData iBeanSetData) {
        //处理editText的textWatcher的问题
        TextWatcher mWacher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                iBeanSetData.setData(s.toString());
            }
        };
        editText.addTextChangedListener(mWacher);
        editText.setTag(mWacher);


    }

    public static void setEditTextWatcher(EditText editText, final IBeanSetData iBeanSetData,
                                          final IBeanGetData iBeanGetData) {
        //处理editText的textWatcher的问题
        editText.removeTextChangedListener((TextWatcher) editText.getTag());
        editText.setText(iBeanGetData.getData());
        TextWatcher mWacher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                iBeanSetData.setData(s.toString());
            }
        };
        editText.addTextChangedListener(mWacher);
        editText.setTag(mWacher);


    }


    public static <T extends View> void setEditTextWatcher(T view, final IBeanSetData iBeanSetData,
                                                           final IBeanGetData iBeanGetData) {
        //1.处理editText的textWatcher的问题
        //2.editText设值
        //3.移除editTextList

        if (view instanceof EditText) {
            ((EditText) view).removeTextChangedListener((TextWatcher) view.getTag());

            ((EditText) view).setText(iBeanGetData.getData());
            TextWatcher mWacher = new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                    iBeanSetData.setData(s.toString());
                }
            };
            ((EditText) view).addTextChangedListener(mWacher);
            ((EditText) view).setTag(mWacher);
        } else if (view instanceof Spinner) {
//            ((Spinner)view).set
        }


    }

    public static void setSpinnerAdp(Spinner spinner, List<String> numbList, Context context) {
        ArrayAdapter<String> operatoAdapter = new ArrayAdapter<String>(context,
                android.R.layout.simple_spinner_dropdown_item, numbList);
        spinner.setAdapter(operatoAdapter);
    }


    public interface IBeanSetData<T> {
        void setData(T s);
    }

    public interface IBeanGetData {
        String getData();
    }

}
